<template>
	<view>
		<uni-swipe-action>
			<uni-swipe-action-item v-for="(item, index) in userAddressList" :key="index" :threshold="0"
				:left-options="options1" :show="item.editing || 'none'" @click="(e) => onOptionsClick(e, index)">
				<view class="bg-white px-5 py-2 border-bottom " @click="onAddressClick(index)">
					<view class="font-md text-dark">
						<text class="content-text">{{ item.realname }}</text>
						<text class="ml-2">{{ item.phone }}</text>
					</view>
					<text class="font-sm text-grey mt-2">{{ item.region + " " + item.detail }}</text>

				</view>
			</uni-swipe-action-item>
		</uni-swipe-action>
	</view>
	<view class="mt-2 text-center">
		<button type="warn" plain size="mini" @tap="onAddaddressClick"> <uni-icons type="plusempty"
				color="warn"></uni-icons>添加地址</button>
	</view>
</template>

<script setup>
defineOptions({
	name: 'Address',
})
import {
	ref,
	onMounted,
	reactive
} from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { useAddressStore } from '@/stores/address.js'
import userHttp from '@/apis/user/userHttp.js'
const addressStore = useAddressStore()
const fromEnum = {
	SECKILL: 1,
	MINE: 2
}
let from = reactive(fromEnum.MINE)
const userAddressList = ref([])
const getAddressList=async ()=>{
	const response=await userHttp.getAddressList()
	userAddressList.value=response.address_list
}

onLoad((options) => {
	uni.$on("address-edit", (params) => {
		if (params.address) {
			if (addressIndex.value === -1) {
				userAddressList.value.push(params.address)
			} else {
				userAddressList.value[addressIndex.value] = params.address
			}
			addressIndex.value = -1
		}
	})
	uni.$on('address-add', (params) => {
			console.log(params);
			let address = params.address
			userAddressList.value.unshift(address); 
	})

	if (options.from === 'seckill') {
		from = fromEnum.SECKILL
	} else {
		from = fromEnum.MINE
	}
	getAddressList()
})

const addressIndex = ref(-1)

const onAddressClick = (index) => {
	if (from === fromEnum.SECKILL) {
		console.log(userAddressList.value[index])

		uni.$emit('choose-address', {
			'address': userAddressList.value[index],
		})
		uni.navigateBack()
	}else{
		setOpened(index)
	}	
}
const options1 = ref([
	{
		text: '删除',
		style: {
			backgroundColor: '#007aff'
		}
	},
	{
		text: '编辑',
		style: {
			backgroundColor: '#F56C6C'
		}
	},
	{
		text: '设为默认',
		style: {
			backgroundColor: '#FF9900'
		}
	}
])
const setOpened = (index) => {
	const item = userAddressList.value[index];
	if (item.editing === 'none') {
		item.editing = 'left';
	} else {
		item.editing = 'none';
	}
}
const onOptionsClick = (e, index) => {
	if (e.index === 0) {
		uni.showModal({
			title: '提示',
			content: '是否删除',
			success: res => {
				if (res.confirm) {
					this.swipeList.splice(index, 1);
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	} else if (e.index === 1) {
		onEditAddressClick(index)
	}
}
const onAddaddressClick = () => {

	addressStore.clearAddress()
	onEditAddressClick(-1)


}
const onEditAddressClick = (index) => {
	addressIndex.value = index
	addressStore.clearAddress()
	addressStore.address = userAddressList.value[index]

	uni.navigateTo({
		url: '/pages/address-edit/address-edit?index=' + index
	})
}





</script>

<style></style>